package com.key.service.impl;

import com.github.pagehelper.PageHelper;
import com.key.common.R;
import com.key.common.dto.ApplyDTO;
import com.key.common.vo.ApplyVO;
import com.key.entity.Apply;
import com.key.mapper.ApplyMapper;
import com.key.service.ApplyService;
import com.key.utils.OddNumbersUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;

/**
 * @author wen
 * @create 2023-02-22 15:56
 */
@Service
public class ApplyServiceImpl implements ApplyService {

    @Autowired
    ApplyMapper applyMapper;

    @Override
    public R findAllApply(ApplyDTO applyDTO) {

        //获取日期范围区间
        if(applyDTO.getApplyTime()!=0){
            applyDTO.setDay1(OddNumbersUtils.getDay());
            applyDTO.setDay2(OddNumbersUtils.getMon(-applyDTO.getApplyTime()));
        }

        //当前用户
//        Subject subject = SecurityUtils.getSubject();
//        String s = String.valueOf(subject.getPrincipal());
//        applyDTO.setApplicant(s);

        //未分页查询，用于count获取
        List<Apply> applyList2= applyMapper.selectApplyByApplyDTO(applyDTO);
        Integer currentPage=applyDTO.getCurrentPage();
        Integer pageSize=applyDTO.getPageSize();
        if (currentPage!=null&&pageSize!=null){
            PageHelper.startPage(currentPage,pageSize);
        }
        List<Apply> applyList= applyMapper.selectApplyByApplyDTO(applyDTO);
        List<ApplyVO> applyVOList=new ArrayList<>();
        for (Apply a:applyList) {
            applyVOList.add(new ApplyVO(a));
        }
        R r=new R();
        r.setMsg("success");
        r.setCount(applyList2.size());
        r.setData(applyVOList);
        return r;
    }

    @Override
    public void creatApply(Apply apply) {
        applyMapper.insertApply(apply);
    }

    @Override
    public R editApplySign(String applyNumber) {
        R r=new R();
        Apply apply =new Apply();
        apply.setApplyNumber(applyNumber);
        //签收日期
        apply.setSignDate(new Timestamp(System.currentTimeMillis()));
        applyMapper.updateApply(apply);
        r.setCode(200);
        r.setMsg("修改成功");
        return r;
    }
}
